#include "rrCintamaniDocument.h"
#include "rrCintamaniDOM.h"
#include "JSObject.h"
#include "variant_list.h"
#include "DOM/Document.h"
#include "global/config.h"
#include "DOM/Window.h"
 
rrCintamaniDocument::rrCintamaniDocument( const FB::DOM::DocumentPtr & Doc ) : mDocument(Doc)
{

}

rrCintamaniDocument::~rrCintamaniDocument()
{

}

rrCintamaniElementPtr rrCintamaniDocument::GetBody() const {
	return rrCintamaniElementPtr( new rrCintamaniElement( mDocument->getBody() ));
}

rrCintamaniElementPtr rrCintamaniDocument::CreateElement(const std::string &name) const {
	FB::JSObjectPtr api = mDocument->callMethod<FB::JSObjectPtr>("createElement", FB::variant_list_of(name));
	return rrCintamaniElementPtr( new rrCintamaniElement(FB::DOM::Element::create(api)));
}

rrCintamaniElementVideoPtr rrCintamaniDocument::CreateElementVideo(const std::string &name ) const {
	FB::JSObjectPtr api = mDocument->callMethod<FB::JSObjectPtr>("createElement", FB::variant_list_of(name));
	return rrCintamaniElementVideoPtr( new rrCintamaniElementVideo(FB::DOM::Element::create(api)));
}

void rrCintamaniDocument::GetElementsByTagName(const std::string &name, std::vector<rrCintamaniElementPtr> & tagList ) const {
	std::vector<FB::DOM::ElementPtr> Elements = mDocument->getElementsByTagName(name);
	for(int i = 0 ; i < Elements.size(); i++ ) {
		tagList.push_back( rrCintamaniElementPtr( new rrCintamaniElement( Elements[i] ) )  );
	}
}

rrCintamaniElementPtr rrCintamaniDocument::GetElementFromPoint( int x, int y )
{
	FB::JSObjectPtr api = mDocument->callMethod<FB::JSObjectPtr>("elementFromPoint", FB::variant_list_of(x)(y));
	return rrCintamaniElementPtr( new rrCintamaniElement(FB::DOM::Element::create(api)));
}

FB::JSObjectPtr rrCintamaniDocument::CreateEvent( const std::string & EventName ) const
{
	FB::JSObjectPtr api = mDocument->callMethod<FB::JSObjectPtr>("createEvent", FB::variant_list_of("HTMLEvents"));
	api->InvokeAsync("initEvent", FB::variant_list_of(EventName)(true)(true));
	return api;
}

bool rrCintamaniDocument::DispatchEvent( const FB::JSObjectPtr & Event )
{
	return mDocument->callMethod<bool>("dispatchEvent", FB::variant_list_of(Event));
}
 